<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="课程名称" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教练名称" prop="coachId">
        <el-select v-model="queryParams.coachId" placeholder="请输入教练名称" clearable>
          <el-option
            v-for="item in coachOptions"
            :key="item.coachId"
            :label="item.coachName"
            :value="item.coachId"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="教练名称" prop="coachId">
        <el-input
          v-model="queryParams.coachId"
          placeholder="请输入教练名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="课程类型" prop="courseType">
        <el-select v-model="queryParams.courseType" placeholder="请选择课程类型" clearable>
          <el-option
            v-for="dict in dict.type.sys_course_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所用器材" prop="courseEquipment">
        <el-select v-model="queryParams.courseEquipment" placeholder="请选择所用器材" clearable>
          <el-option
            v-for="dict in dict.type.sys_equipment"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="健身部位" prop="courseBodyParts">
        <el-select v-model="queryParams.courseBodyParts" placeholder="请选择健身部位" clearable>
          <el-option
            v-for="dict in dict.type.sys_body_part"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="难度" prop="courseDifficulty">
        <el-select v-model="queryParams.courseDifficulty" placeholder="请选择难度" clearable>
          <el-option
            v-for="dict in dict.type.sys_difficulty"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item style="float: right;">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:courses:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:courses:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:courses:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:courses:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="coursesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="60" align="center" />
      <el-table-column align="center" type="index" />
      <el-table-column label="课程名称" align="center" prop="courseName" width="120px" />
      <el-table-column label="教练名称" align="center" prop="coachId" >
          <template slot-scope="scope">
            <span>{{ scope.row.coachId?coachOptions.find(item => item.coachId == scope.row.coachId).coachName:'' }}</span>
          </template>
      </el-table-column>
      <!-- <el-table-column label="教练名称" align="center" prop="coachId" /> -->
      
      <el-table-column label="课程类型" align="center" prop="courseType" width="120px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_course_type" :value="scope.row.courseType"/>
        </template>
      </el-table-column>
      <el-table-column label="所用器材" align="center" prop="courseEquipment">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_equipment" :value="scope.row.courseEquipment"/>
        </template>
      </el-table-column>
      <el-table-column label="健身部位" align="center" prop="courseBodyParts">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_body_part" :value="scope.row.courseBodyParts"/>
        </template>
      </el-table-column>
      <el-table-column label="课程时长(min)" align="center" prop="courseDuration" />
      <el-table-column label="难度" align="center" prop="courseDifficulty">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_difficulty" :value="scope.row.courseDifficulty"/>
        </template>
      </el-table-column>
      <el-table-column label="预计消耗卡路里(千卡)" align="center" prop="courseCalories" width="150px"/>
      <el-table-column label="课程描述" align="center" prop="courseDescription" width="190px"/>
      <el-table-column label="课程视频" align="center" prop="courseVedio" width="150" />
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleApplication(scope.row)"
            v-hasPermi="['system:courses:application']"
          >报名</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:courses:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:courses:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改健身课程信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-row>
          <el-col :span = "12">
            <el-form-item label="课程名称" prop="courseName">
              <el-input v-model="form.courseName" placeholder="请输入课程名称" />
            </el-form-item>
          </el-col>
          <el-col :span = "12">
            <el-form-item label="教练名称" prop="coachId">
              <el-select v-model="form.coachId" placeholder="请输入教练名称" clearable style="width: 100%;">
                <el-option
                  v-for="item in coachOptions"
                  :key="item.coachId"
                  :label="item.coachName"
                  :value="item.coachId"
                />
              </el-select>
            </el-form-item>
            <!-- <el-form-item label="教练名称" prop="coachId">
              <el-input v-model="form.coachId" placeholder="请输入教练名称" />
            </el-form-item> -->
          </el-col>
        </el-row>
        
        <el-row>
          <el-col :span = "12">
            <el-form-item label="课程类型" prop="courseType">
              <el-select v-model="form.courseType" placeholder="请选择课程类型" style="width: 100%;">
                <el-option
                  v-for="dict in dict.type.sys_course_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span = "12">
            <el-form-item label="所用器材" prop="courseEquipment">
              <el-select v-model="form.courseEquipment" placeholder="请选择所用器材" style="width: 100%;">
                <el-option
                  v-for="dict in dict.type.sys_equipment"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span = "12">
            <el-form-item label="健身部位" prop="courseBodyParts">
              <el-select v-model="form.courseBodyParts" placeholder="请选择健身部位" style="width: 100%;">
                <el-option
                  v-for="dict in dict.type.sys_body_part"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span = "12">
            <el-form-item label="课程时长(min)" prop="courseDuration">
              <el-input v-model="form.courseDuration" placeholder="请输入课程时长" style="width: 100%;" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="难度" prop="courseDifficulty">
              <el-select v-model="form.courseDifficulty" placeholder="请选择难度" style="width: 100%;">
                <el-option
                  v-for="dict in dict.type.sys_difficulty"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="消耗卡路里(千卡)" prop="courseCalories">
              <el-input v-model="form.courseCalories" placeholder="请输入消耗卡路里(千卡)" style="width: 100%;" />
            </el-form-item>
          </el-col>
        </el-row> 
        <el-form-item label="课程描述" prop="courseDescription">
          <el-input v-model="form.courseDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="课程视频">
          <editor v-model="form.courseVedio" :min-height="192"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCourses, getCourses, delCourses, addCourses, updateCourses } from "@/api/system/courses";
import { listCoaches } from "@/api/system/coaches"
import { addCourseregistrations } from "@/api/system/courseregistrations"

export default {
  name: "Courses",
  dicts: ['sys_course_type', 'sys_difficulty', 'sys_body_part', 'sys_equipment'],
  data() {
    return {
      //教练信息
      coachOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 健身课程信息表格数据
      coursesList: [],
      //教练列表
      coachList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseName: null,
        coachId: null,
        courseType: null,
        courseEquipment: null,
        courseBodyParts: null,
        courseDifficulty: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        courseName: [
          { required: true, message: "课程名称不能为空", trigger: "blur" }
        ],
        coachId: [
          { required: true, message: "教练ID不能为空", trigger: "blur" }
        ],
        courseType: [
          { required: true, message: "课程类型不能为空", trigger: "change" }
        ],
        courseEquipment: [
          { required: true, message: "所用器材不能为空", trigger: "change" }
        ],
        courseBodyParts: [
          { required: true, message: "健身部位不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getCoach();
  },
  methods: {
    /** 查询教练信息 */
    getCoach(){
      const params = {
        pageNum:1,
        pageSize:0,
      }
      listCoaches(this.queryParams).then(response => {
        this.coachOptions = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询健身课程信息列表 */
    getList() {
      this.loading = true;
      listCourses(this.queryParams).then(response => {
        this.coursesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //移除HTML标签
    delTag(str){
      return str.replace(/<[^>]+>/g,"");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        courseId: null,
        courseName: null,
        coachId: null,
        courseDescription: null,
        courseType: null,
        courseEquipment: null,
        courseBodyParts: null,
        courseDuration: null,
        courseDifficulty: null,
        courseCalories: null,
        courseVedio: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.coachList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.courseId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加健身课程信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const courseId = row.courseId || this.ids
      getCourses(courseId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改健身课程信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.courseId != null) {
            updateCourses(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCourses(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const courseIds = row.courseId || this.ids;
      this.$modal.confirm('是否确认删除健身课程信息编号为"' + courseIds + '"的数据项？').then(function() {
        return delCourses(courseIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleApplication(row){
      const courseIds = row.courseId || this.ids;
      const courseName = row.courseName || this.ids;
      var now = new Date();
      const params ={
        userId:this.$store.state.user.id,
        courseId: row.courseId,
        registrationDatetime:now,
        registrationStatus:"state2",
      }
      this.$modal.confirm('是否确认报名健身课程名称为"' + courseName + '"的数据项？').then(function() {
        return addCourseregistrations(params).then(res=>{
                  console.log('res',res)
                  console.log("row",row)
                  console.log(params)
                });
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("报名成功,待管理员审核");
      }).catch(() => {});
      // addCourseregistrations(params).then(res=>{
      //   console.log('res',res)
      // })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/courses/export', {
        ...this.queryParams
      }, `courses_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
